package test;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

class ColoredCells extends JFrame
{
  ColoredCells (String title)
  {
   // Pass the title to the JFrame superclass so that it appears in
   // the title bar.

   super (title);

   // Tell the program to exit when the user either selects Close
   // from the System menu or presses an appropriate X button on the
   // title bar.

   setDefaultCloseOperation (EXIT_ON_CLOSE);

   // Create a default table model consisting of headers columns
   // and 5 rows.

   String [] headers = { "Name", "Balance" };

   DefaultTableModel dtm = new DefaultTableModel (headers, 5);

   // Populate all cells in the default table model.

   String [] names =
   {
     "John Doe",
     "Jane Smith",
     "Jack Jones",
     "Paul Finch",
     "Susan Smyth"
   };

   double [] balances =
   {
     -50000.0,
     4000.0,
     -32.3,
     802.5,
     -128.0
   };

   int nrows = dtm.getRowCount ();
   int ncols = dtm.getColumnCount ();

   for (int i = 0; i < nrows; i++)
   {
      dtm.setValueAt (names [i], i, 0);
      dtm.setValueAt (new Double (balances [i]), i, 1);
   }

   // Create a table using the previously created default table
   // model.

   JTable jt = new JTable (dtm);

   // Create a renderer for displaying cells in certain colors.

   TableRenderer tr = new TableRenderer ();

   // Get the column model so we can extract individual columns.

   TableColumnModel tcm = jt.getColumnModel ();

   // For each table column, sets its renderer to the previously 
   // created table renderer.

   for (int c = 0; c < ncols; c++)
   {
      TableColumn tc = tcm.getColumn (c);
      tc.setCellRenderer (tr);
   }

   // Place the table in a JScrollPane object (to allow the table to
   // be vertically scrolled and display scrollbars, as necessary).

   JScrollPane jsp = new JScrollPane (jt);

   // Add the JScrollPane object to the frame window's content pane.
   // That allows the table to be displayed within a displayed 
   // scroll pane.

   getContentPane ().add (jsp);

   // Establish the overall size of the frame window to 200
   // horizontal pixels by 130 vertical pixels.

   setSize (200, 130);

   // Display the frame window and all contained
   // components/containers.

   setVisible (true);
  }

  public static void main (String [] args)
  {
   // Create a ColoredCells object, which creates the GUI.

   new ColoredCells ("Colored Cells");
  }
}

class TableRenderer extends DefaultTableCellRenderer
{
  private int row, col;

  public Component getTableCellRendererComponent (JTable table,
                          Object value,
                          boolean isSelected,
                          boolean hasFocus,
                          int row,
                          int column)
  {
   // Save row and column information for use in setValue().

   this.row = row;
   this.col = column;

   // Allow superclass to return rendering component.

   return super.getTableCellRendererComponent (table, value, 
                         isSelected, hasFocus,
                         row, column);
  }

  protected void setValue (Object v)
  {
   // Allow superclass to set the value.

   super.setValue (v);

   // If in names column, color cell with even row number white on
   // dark green, and cell with odd row number black on white.

   if (col == 0)
   {
     if (row % 2 == 0)
     {
       setForeground (Color.white);
       setBackground (new Color (0, 128, 0));
     }
     else
     {
       setForeground (UIManager.getColor ("Table.foreground"));
       setBackground (UIManager.getColor ("Table.background"));
     }

     return;
   }

   // Must be in balances column. Make sure v is valid.

   if (v == null)
     return;
    
   // Extract the cell's numeric value.

   Double d = (Double) v;

   // If numeric value is less than zero, color cell yellow on red.
   // Otherwise, color cell black on white.

   if (d.doubleValue () < 0)
   {
     setForeground (Color.yellow);
     setBackground (Color.red);
   }
   else
   {
     setForeground (UIManager.getColor ("Table.foreground"));
     setBackground (UIManager.getColor ("Table.background"));
   }
  }
}